<template>
  <div class="recommend-list">
    <div class="recommend-card" v-for="(item, index) in videoList" :key="index">
      <el-card class="recommend-video-card">
        <div>
          <el-image :src="item.img | coverUrl" lazy class="recommend-pic" @click="selectItem(item)"></el-image>
        </div>
        <div class="recommend-title">
          <span>{{item.title}}</span>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { coverUrl } from "@/utils/url";
import { getRecommendList } from "@/api/videoService";
export default {
  props: {},
  data() {
    return {
      videoList: [],
      page: 1,
      page_size: 8,
      flag: true, //视频在第一页
    };
  },
  methods: {
    selectItem(item) {
      this.$router.push({ name: "video", params: { vid: item.vid } });
    },
    _getRecommendList() {
      getRecommendList(this.page, this.page_size).then((res) => {
        if (res.data.code === 200) {
          if (this.flag) {
            this.videoList = res.data.data.data;
            this.flag = !this.flag;
          } else {
            var newList = res.data.data.data;
            if (newList.length == 0) {
              this.$message.info("没有更多了");
            } else {
              this.videoList = this.videoList.concat(newList);
            }
          }
        }
      });
    },
    getMore() {
      this.page++;
      this._getRecommendList();
    },
  },
  created() {
    this._getRecommendList();
  },
  filters: {
    coverUrl(name) {
      return coverUrl(name);
    },
  },
};
</script>

<style>
.recommend-list {
  display: flex;
  flex-wrap: wrap;
  margin-top: 15px;
}

.recommend-card {
  display: inline-block;
  width: 23%;
  padding: 8px 5px;
  box-sizing: border-box;
  margin-left: 30px;
}

.recommend-video-card {
  height: 330px;
}

.recommend-pic {
  top: 0px;
  left: 0px;
  width: 100%;
  height: 240px;
}

.recommend-title {
  height: 20px;
  margin: 0;
  font-family: "Microsoft YaHei";
  font-size: 18px;
  line-height: 20px;
  color: #212121;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
}
</style>